<script setup lang="ts">
import { getCurrentInstance, ref, reactive, onMounted } from 'vue'
import { onBeforeRouteLeave } from 'vue-router'
const { proxy } = getCurrentInstance()

/* ---- */
const userInfo = proxy.$Store.get('userInfo') || {}
const WXConfig = proxy.$Store.get('WXConfig') || {}
const forSys = proxy.$Sys || []

/* --设置微信分享功能-- */
proxy.$WxTool.loadJsdk(WXConfig, (obj) => {
  if (!obj) return
  // ----
  obj.hideMenuItem()
})

/* ---- */
const share = reactive({
  add: true,
  maxnum: 1,
  forFree: 1,
  setTrueStyle: {},
  imgs: [],
  option: {
    viewMode: 0,
    dragMode: 'move',
    aspectRatio: 1,
    autoCropArea: 1,
    center: false,
    cropBoxMovable: false,
    cropBoxResizable: false,
    toggleDragModeOnDblclick: false
  },
  cropImg: proxy.$Global.cropImg
})
const floatAdv = reactive({
  add: true,
  maxnum: 1,
  forFree: 9 / 5,
  setTrueStyle: {},
  imgs: [],
  option: {
    viewMode: 0,
    dragMode: 'move',
    autoCropArea: 1,
    center: false,
    cropBoxMovable: false,
    cropBoxResizable: false,
    toggleDragModeOnDblclick: false
  },
  cropImg: proxy.$Global.cropImg
})
// ----
let globalData = {}
// ----
const form = reactive({
  id: 0,
  tabName: 'market_clubmember',
  // ----
  titleName: '',
  shareName: '',
  shareDesc: '',
  remark: '',
  remarkyes: ''
})

/* ---- */
const uploadoss = ref()
const layer = ref()
const layerLoading = ref()

/* ---- */
const getFormInfo = () => {
  share.setTrueStyle = { height: `${document.querySelector('.ratioBox dl dd label').clientWidth / share.forFree}px` }
  floatAdv.setTrueStyle = { height: `${document.querySelector('.powerBox dl dd label').clientWidth / floatAdv.forFree}px` }
  // -- 获取信息 --
  const newItems = forSys.filter((item) => { return String(item.name) === form.tabName })
  if (Number(newItems.length) === 1) {
    form.id = newItems[0].id
    proxy.$Myfun.getJsonData(newItems[0].value, (resJson) => {
      const RJData = resJson[0] || {}
      globalData = RJData
      // ----
      if (RJData.title) form.titleName = RJData.title
      form.shareName = RJData.share_title
      form.shareDesc = RJData.share_desc
      form.remark = RJData.remark
      form.remarkyes = RJData.remarkyes
      // ----
      RJData.share_img.forEach((item) => {
        share.imgs.push({
          imgName: item.name,
          imgUrl: item.image,
          imgW: item.width,
          imgH: item.height
        })
      })
      if (Number(share.imgs.length) >= Number(share.maxnum) && Number(share.maxnum) > 0) share.add = !share.add
      // ----
      RJData.float_img.forEach((item) => {
        floatAdv.imgs.push({
          imgName: item.name,
          imgUrl: item.image,
          imgW: item.width,
          imgH: item.height
        })
      })
      if (Number(floatAdv.imgs.length) >= Number(floatAdv.maxnum) && Number(floatAdv.maxnum) > 0) floatAdv.add = !floatAdv.add
    })
  }
}
// --上传图片操作--
const uploadAct = (type, selectObj, event, index) => {
  const uploadOss = uploadoss.value
  switch (Number(type)) {
    case 1: {
      uploadOss.uploadImg(event, selectObj,'imgs', index)
      break
    }
    case 2: {
      uploadOss.delInfo(selectObj, event)
      break
    }
  }
}
// ----
const subform = async () => {
  if (Number(form.titleName.length) === 0) {
    proxy.$Myfun.forToast(layer.value, 'open', '请填写页面标题！')
    return false
  }
  // ----
  const shareItems = []
  if (Number(share.imgs.length) > 0) {
    share.imgs.forEach((item, index) => {
      shareItems.push({
        name: item.imgName,
        image: item.imgUrl,
        width: item.imgW,
        height: item.imgH
      })
    })
  }
  // ----
  const floatItems = []
  if (Number(floatAdv.imgs.length) > 0) {
    floatAdv.imgs.forEach((item) => {
      floatItems.push({
        name: item.imgName,
        image: item.imgUrl,
        width: item.imgW,
        height: item.imgH
      })
    })
  }
  // ----
  globalData.title = form.titleName
  globalData.share_title = form.shareName
  globalData.share_desc = form.shareDesc
  globalData.share_img = shareItems
  globalData.float_img = floatItems
  globalData.remark = form.remark
  globalData.remarkyes = form.remarkyes
  // ----
  const forValue = JSON.stringify(globalData)
  // ----
  let forType = 'post'
  let forUrl = 'system/create'
  let forData = {
    name: form.tabName,
    value: forValue,
    // ----
    config_id: userInfo.config_id,
    openid: userInfo.openid
  }
  if (Number(form.id) > 0) {
    forType = 'patch'
    forUrl = 'system/update?id=' + form.id
    forData = {
      value: forValue,
      // ----
      config_id: userInfo.config_id,
      openid: userInfo.openid
    }
  }
  // ----
  proxy.$Myfun.onLoading(layerLoading.value, 'open', proxy.$Global.loadingMsg)
  let resData = ''
  await proxy.$http[forType](forUrl, forData).then((res) => { resData = res }).catch((err) => { proxy.$Myfun.forToast(layer.value, 'open', err) })
  // ----
  proxy.$Myfun.disLoading(layerLoading.value, 'close')
  if (resData) proxy.$router.back()
}
const goback = () => { proxy.$router.back() }

/* ---- */
onMounted(() => {
  getFormInfo()
})
onBeforeRouteLeave(() => { proxy.$Myfun.setScrollTop(proxy.$refs.formSection) })
</script>

<template>
  <div class="mescroll-touch" ref="formSection">
    <div class="formSet">
      <ul>
        <li>
          <div class="form-are xw-radius-3">
            <h1>页面标题</h1>
            <p><textarea class="tarea-H2" placeholder="" v-model.trim="form.titleName"></textarea></p>
          </div>
        </li>
        <li>
          <div class="form-are xw-radius-3">
            <h1>分享标题</h1>
            <p><textarea class="tarea-H2" placeholder="" v-model.trim="form.shareName"></textarea></p>
          </div>
        </li>
        <li>
          <div class="form-are xw-radius-3">
            <h1>分享描述</h1>
            <p><textarea class="tarea-H3" placeholder="" v-model.trim="form.shareDesc"></textarea></p>
          </div>
        </li>
        <li>
          <div class="form-imgs xw-radius-3 ratioBox">
            <div class="FIS-top xw-flex xw-flex-align xw-flex-justify">
              <span class="FIS-title">分享图标</span>
            </div>
            <dl class="FIS-img">
              <dd v-for="(item, index) in share.imgs"><label class="xw-block xw-act"><img :src="item.imgUrl"/><input type="file" style="display: none;" accept="image/*" @change="uploadAct(1, share, $event, index)"/></label><span class="FIS-del fal fa-times xw-act" @click="uploadAct(2, share, index)"></span></dd>
              <dd v-if="share.add"><label class="xw-act xw-flex xw-flex-center" :style="share.setTrueStyle"><em class="fal fa-plus"></em><input type="file" style="display: none;" accept="image/*" @change="uploadAct(1, share, $event)"/></label></dd>
            </dl>
          </div>
        </li>
        <li>
          <div class="form-imgs xw-radius-3 powerBox">
            <div class="FIS-top xw-flex xw-flex-align xw-flex-justify">
              <span class="FIS-title">漂浮广告图片</span>
            </div>
            <dl class="FIS-img">
              <dd v-for="(item, index) in floatAdv.imgs"><label class="xw-block xw-act"><img :src="item.imgUrl"/><input type="file" style="display: none;" accept="image/*" @change="uploadAct(1, floatAdv, $event, index)"/></label><span class="FIS-del fal fa-times xw-act" @click="uploadAct(2, floatAdv, index)"></span></dd>
              <dd v-if="floatAdv.add"><label class="xw-act xw-flex xw-flex-center" :style="floatAdv.setTrueStyle"><em class="fal fa-plus"></em><input type="file" style="display: none;" accept="image/*" @change="uploadAct(1, floatAdv, $event)"/></label></dd>
            </dl>
          </div>
        </li>
        <li>
          <div class="form-are xw-radius-3">
            <h1>入会前文字广告</h1>
            <p><textarea class="tarea-H3" placeholder="" v-model.trim="form.remark"></textarea></p>
          </div>
        </li>
        <li>
          <div class="form-are xw-radius-3">
            <h1>入会后文字广告</h1>
            <p><textarea class="tarea-H3" placeholder="" v-model.trim="form.remarkyes"></textarea></p>
          </div>
        </li>
      </ul>
      <div class="form-sub xw-radius-3 xw-bgCL-11 xw-act" @click="subform">确定提交</div>
      <div class="form-sub xw-radius-3 xw-WhiteRed xw-act" @click="goback">取消返回</div>
    </div>
    <uploadoss-view ref="uploadoss"/>
    <layer-view ref="layer"/>
    <layer-view ref="layerLoading"/>
  </div>
</template>
